%% Solar System

%clean up
clear all
close all
clear classes

%% Here, we consider only two planets
 
Earth=Planet()
Earth.a=1;
Earth.epsilon=0.0167;
Earth.dt=.01
Earth.Period=1;
Earth.calcOrbit()
Earth.plotOrbit()

Mars=Planet()
Mars.a=1.52;
Mars.epsilon=0.0935;
Mars.dt=.01
Mars.Period=1.881;
Mars.calcOrbit()
Mars.plotOrbit()

% %Setup movie
filename='KeplerOrbits.mp4'
VidObj=VideoWriter(filename,'MPEG-4')

%Set FrameRate property
 VidObj.FrameRate=1/Earth.dt
 
% %Open video file for writing
 VidObj.open();
 fH=figure
 xmax=2
 for ii=1:length(Earth.Tvec) % Choose the the smallest Planet.Period to show the movie 
                             % In this case Earth.Period < Mars.Period
     
     %Plot the position of the Planets
     plot3(Earth.X(1,ii),Earth.X(2,ii),Earth.X(3,ii),'go','linewidth',2)
     hold on
     plot3(Mars.X(1,ii),Mars.X(2,ii),Mars.X(3,ii),'ro','linewidth',2)
     
     %Draw velocity vectors
     quiver(Earth.X(1,ii),Earth.X(2,ii),Earth.X(4,ii)/10,Earth.X(5,ii)/10,0,'b','linewidth',.5) 
     quiver(Mars.X(1,ii),Mars.X(2,ii),Mars.X(4,ii)/10,Mars.X(5,ii)/10,0,'b','linewidth',.5) 
     
     %Draw Angular momentum vector
     % L=r cross p 
     % Lx=Ly=0  
     X_Earth= Earth.X(1:3,ii);
     P_Earth=Earth.X(4:6,ii);
     L_Earth=cross(X_Earth,P_Earth);
     quiver3(Earth.X(1,ii),Earth.X(2,ii),Earth.X(3,ii),0,0,L_Earth(3)/10,'k','linewidth',1)
     
     X_Mars= Mars.X(1:3,ii);
     P_Mars=Mars.X(4:6,ii);
     L_Mars=cross(X_Mars,P_Mars);
     quiver3(Mars.X(1,ii),Mars.X(2,ii),Mars.X(3,ii),0,0,L_Mars(3)/10,'k','linewidth',1)

     % Draw Sun  
     % As I solved it in Center of Mass coordinate, it means I put the Sun
     % at origin and use reduced mass for each planet is rotating around
     % Sun
     plot3(0,0,0,'yo','linewidth',10)
     
     axis([-xmax xmax -xmax xmax -xmax xmax])
     hold off    %so new plot will won't add to this one
       
     xlabel('X (AU)')
     ylabel('Y (AU)')
     title('Solar System')
     legend('Earth','Mars','Velocity(AU/10Year)','','Angular momentum','','Sun')
     
     %now add frame to movie 
     currFrame = getframe();
     VidObj.writeVideo(currFrame);
     
 end
  
 %close video file
 VidObj.close()



 

